package com.aqielife.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author aqie
 * @date 2022/02/05 12:28
 * @desc
 */
@Data
public class R<T> implements Serializable {

  private int code;

  private boolean success;

  private T data;

  private String msg;

  private R(int code, T data, String msg) {
    this.code = code;
    this.data = data;
    this.msg = msg;
    this.success = 200 == code;
  }

  public static <T> R<T> data(T data) {
    return data(data, "success");
  }

  public static <T> R<T> data(T data, String msg) {
    return data(200, data, msg);
  }
  public static <T> R<T> data(int code, T data, String msg) {
    return new R<>(code, data, data == null ? "暂无承载数据" : msg);
  }

  public static <T> R<T> ok(String msg) {
    return data(null, msg);
  }

  public static <T> R<T> fail(String msg) {
    return data(400, null, msg);
  }
}
